/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package italk.ui.vidw.panel;

import italk.client.bean.State.ContactState;
import italk.client.entity.Category;
import italk.client.entity.Contact;
import italk.client.entity.Group;
import italk.client.service.CategoryService;
import italk.client.service.ContactService;
import italk.client.service.GroupService;
import italk.ui.InfoDialog;
import italk.ui.bean.CellPanel;
import italk.ui.bean.ComboBoxImageItem;
import italk.ui.renderer.IconListRenderer;
import italk.ui.renderer.PanelCellRender;
import italk.ui.view.ConversationFacetory;
import italk.ui.view.ViewFactory;
import italk.ui.widget.SwitchPane;
import italk.util.ImageManager;
import italk.util.LanguageLoader;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.util.List;

import javax.swing.DefaultListModel;
import javax.swing.Icon;
import javax.swing.JList;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.ListModel;

/**
 *
 * @author chenzhihui
 */
public class MainPanel extends javax.swing.JPanel {

    /**
     * Creates new form MainPanel
     */
    public MainPanel() {
        //初始化面板组件
        initComponents();
        //初始化
        init();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        topPanel = new javax.swing.JPanel();
        jPanel4 = new javax.swing.JPanel();
        lblFace = new javax.swing.JLabel();
        jPanel5 = new javax.swing.JPanel();
        lblName = new javax.swing.JLabel();
        comboStatus = new javax.swing.JComboBox();
        lblInfo = new javax.swing.JLabel();
        containerPanel = new javax.swing.JPanel();
        tabbedPane = new javax.swing.JTabbedPane();
        contactPanel = new javax.swing.JPanel();
        groupPanel = new javax.swing.JPanel();
        recentPanel = new javax.swing.JPanel();
        buttomPanel = new javax.swing.JPanel();
        textSearch = new javax.swing.JTextField();
        jToolBar1 = new javax.swing.JToolBar();
        jSeparator1 = new javax.swing.JToolBar.Separator();
        jButton1 = new javax.swing.JButton();

        setPreferredSize(new java.awt.Dimension(255, 555));
        setLayout(new java.awt.BorderLayout());

        topPanel.setPreferredSize(new java.awt.Dimension(255, 80));

        lblFace.setIcon(new javax.swing.ImageIcon(getClass().getResource("/italk/resource/6208317.jpg"))); // NOI18N
        lblFace.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lblFaceMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addGap(23, 23, 23)
                .addComponent(lblFace)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addComponent(lblFace)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        lblName.setFont(lblName.getFont().deriveFont(lblName.getFont().getSize()+3f));
        lblName.setText("承$诺");

        lblInfo.setText("世界上最可怕的事是，比你聪明的人却比你还努力。 ");

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblName)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 29, Short.MAX_VALUE)
                .addComponent(comboStatus, javax.swing.GroupLayout.PREFERRED_SIZE, 57, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(33, 33, 33))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel5Layout.createSequentialGroup()
                .addComponent(lblInfo, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(comboStatus, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel5Layout.createSequentialGroup()
                        .addGap(3, 3, 3)
                        .addComponent(lblName, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(lblInfo)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout topPanelLayout = new javax.swing.GroupLayout(topPanel);
        topPanel.setLayout(topPanelLayout);
        topPanelLayout.setHorizontalGroup(
            topPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(topPanelLayout.createSequentialGroup()
                .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        topPanelLayout.setVerticalGroup(
            topPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jPanel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        add(topPanel, java.awt.BorderLayout.PAGE_START);

        containerPanel.setLayout(new java.awt.BorderLayout());

        contactPanel.setAutoscrolls(true);
        contactPanel.setLayout(new java.awt.BorderLayout(30, 30));
        tabbedPane.addTab("Contacts", contactPanel);

        groupPanel.setLayout(new java.awt.BorderLayout());
        tabbedPane.addTab("Groups", groupPanel);

        recentPanel.setLayout(new java.awt.BorderLayout());
        tabbedPane.addTab("Recently", recentPanel);

        containerPanel.add(tabbedPane, java.awt.BorderLayout.CENTER);

        add(containerPanel, java.awt.BorderLayout.CENTER);

        buttomPanel.setPreferredSize(new java.awt.Dimension(255, 55));
        buttomPanel.setLayout(new java.awt.BorderLayout());

        textSearch.setPreferredSize(new java.awt.Dimension(255, 25));
        buttomPanel.add(textSearch, java.awt.BorderLayout.CENTER);

        jToolBar1.setFloatable(false);
        jToolBar1.setRollover(true);
        jToolBar1.add(jSeparator1);

        jButton1.setText("菜单");
        jButton1.setFocusable(false);
        jButton1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton1.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(jButton1);

        buttomPanel.add(jToolBar1, java.awt.BorderLayout.PAGE_END);

        add(buttomPanel, java.awt.BorderLayout.PAGE_END);
    }// </editor-fold>//GEN-END:initComponents

    private void lblFaceMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lblFaceMouseClicked
        // TODO add your handling code here:
        InfoDialog info = ViewFactory.getInfoDialog();
        info.setVisible(true);
    }//GEN-LAST:event_lblFaceMouseClicked

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel buttomPanel;
    private javax.swing.JComboBox comboStatus;
    private javax.swing.JPanel contactPanel;
    private javax.swing.JPanel containerPanel;
    private javax.swing.JPanel groupPanel;
    private javax.swing.JButton jButton1;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JToolBar.Separator jSeparator1;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JLabel lblFace;
    private javax.swing.JLabel lblInfo;
    private javax.swing.JLabel lblName;
    private javax.swing.JPanel recentPanel;
    private javax.swing.JTabbedPane tabbedPane;
    private javax.swing.JTextField textSearch;
    private javax.swing.JPanel topPanel;
    // End of variables declaration//GEN-END:variables

	/* 
	 * (non-Javadoc)
	 */
	public void init() {
		// TODO Auto-generated method stub

        //初始化顶部面板
        initTopPanel();

        //初始化内容面板
        initContainerPanel();

        //初始化底部面板
        initBottomPanel();
	}

	 /**
     * 初始化顶部面板
     */
    private void initTopPanel() {
        //设置登录状态
    	List<ContactState> status = ContactState.getBuddyList();
        ComboBoxImageItem statusItem = null;
        this.comboStatus.setRenderer(new IconListRenderer());
        String extension = ".png";
        String iconDir = "status" + File.separator;
        for (ContactState state : status) {
            statusItem = new ComboBoxImageItem(state.toString(), iconDir + ContactState.toLowerCase(state) + extension);
            comboStatus.addItem(statusItem);
        }
        comboStatus.setPreferredSize(new Dimension(40, 28));

    }

    /**
     * 初始化内容面板
     */
    private void initContainerPanel() {
        
        
    }

    /**
     * 初始化底部面板
     */
    private void initBottomPanel() {
        //初始化搜索文本框
        JPopupMenu popupMenu = new JPopupMenu();
        JMenuItem id = new JMenuItem("ID");
        JMenuItem name = new JMenuItem("Name");
        JMenuItem email = new JMenuItem("Email");
        popupMenu.add(id);
        popupMenu.add(name);
        popupMenu.add(email);
        textSearch.putClientProperty("JTextField.variant", "search");
        textSearch.putClientProperty("JTextField.Search.PlaceholderText", "Search right here");
        textSearch.putClientProperty("JTextField.Search.FindPopup", popupMenu);
        textSearch.putClientProperty("JTextField.Search.CancelAction", new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                System.out.println("JTextField.Search.CancelAction");
            }
        });

        textSearch.putClientProperty("JTextField.Search.FindAction", new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                System.out.println("JTextField.Search.FindAction");
            }
        });
    }
    
    /**
     * 初始化联系人
     */
    public void initContacts() {
    	//联系人列表
    	SwitchPane fp = new SwitchPane();
        Icon icon = ImageManager.getImageIconByShortName("icons" + File.separator + "contact.png");
        List<Category> categoryList = CategoryService.getCategoryList();
        for(Category c : categoryList) {
        	DefaultListModel model = new DefaultListModel();
        	for(Contact contact : c.getContactList()) {
            	model.addElement(new CellPanel().setObj(contact));
        	}
        	JList list = new JList(model);
        	list.setCellRenderer(new PanelCellRender());
        	fp.addPane(c.getName(), icon, new JScrollPane(list));
        	setListListener(list);
        }
        this.contactPanel.add(fp);
    }
    
    /**
     * 初始化群与讨论组
     */
    public void initGroups() {
    	//群列表
        SwitchPane groupfp = new SwitchPane();
        Icon groupIcon = ImageManager.getImageIconByShortName("icons" + File.separator + "group.png");
        DefaultListModel model = new DefaultListModel();
    	for(Group group : GroupService.getGroupList()) {
        	model.addElement(new CellPanel().setObj(group));
    	}
    	JList list = new JList(model);
    	list.setCellRenderer(new PanelCellRender());
    	setListListener(list);
        
        groupfp.addPane(LanguageLoader.getString("群组"), groupIcon, new JScrollPane(list));
        
        //讨论群列表
        DefaultListModel modelDis = new DefaultListModel();
    	for(Group group : GroupService.getDisussList()) {
    		modelDis.addElement(new CellPanel().setObj(group));
    	}
    	JList listDis = new JList(modelDis);
    	listDis.setCellRenderer(new PanelCellRender());
    	setListListener(listDis);
        groupfp.addPane(LanguageLoader.getString("讨论组"), groupIcon, new JScrollPane(listDis));
        
        //添加到面板中显示
        this.groupPanel.add(groupfp);
    }

    /**
     * 初始化最近联系人
     */
    public void initRecentContacts() {
    	DefaultListModel model = new DefaultListModel();
    	for(Contact contact : ContactService.getRecentList()) {
        	model.addElement(new CellPanel().setObj(contact));
    	}
    	JList list = new JList(model);
    	list.setCellRenderer(new PanelCellRender());
    	setListListener(list);
    	this.recentPanel.add(new JScrollPane(list));
    }
    
    private void setListListener(final JList list){
    	list.addMouseListener(new MouseAdapter() {
    		/* (non-Javadoc)
    		 * @see java.awt.event.MouseAdapter#mouseClicked(java.awt.event.MouseEvent)
    		 */
    		@Override
    		public void mouseClicked(MouseEvent e) {
    			// TODO Auto-generated method stub
    			if(e.getClickCount() == 2) {
    				int index = list.locationToIndex(e.getPoint());
        			ListModel model = list.getModel();
        			CellPanel contactPanel = (CellPanel)model.getElementAt(index);
        			ConversationFacetory.createConversation(contactPanel.getObj());
    			}
    		}
		});
    }
    
}